package com.be.beadmin.design.controller;



import com.alibaba.excel.EasyExcelFactory;
import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.be.beadmin.common.exception.BeadminException;
import com.be.beadmin.design.dto.input.UnStandProductBopAndProcessDto;
import com.be.beadmin.design.dto.input.UnStandProductProcessInfoQueryCriteria;
import com.be.beadmin.design.dto.output.UnStandProductWorkHourAndCycleEx;
import com.be.beadmin.design.util.ExcelListener;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import com.be.beadmin.design.service.UnStandProductProcessInfoService;
import com.be.beadmin.common.utils.ResultModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.excel.EasyExcel;

import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.io.IOException;
import java.util.List;

import org.springframework.web.multipart.MultipartFile;

/**
* @author cms
* @date 2023-09-13
**/

@Api(tags = {"设计管理-非标准柜体工艺工序工时"})
@RestController
public class UnStandProductProcessInfoController {

    @Autowired
    private  UnStandProductProcessInfoService unStandProductProcessInfoService;


    @ApiOperation("增加非标柜体工艺工序工时")
    @PostMapping("/design/addUnStandProductProcessInfo")
    public ResultModel addStandProductProcessInfo(@RequestBody UnStandProductBopAndProcessDto unStandProductBopAndProcessDto){

        unStandProductProcessInfoService.addUnStandProductBopAndProcessDto(unStandProductBopAndProcessDto);
        return ResultModel.success("设置成功");
    }

    @ApiOperation("不分页获取非标柜体工艺工序工时")
    @PostMapping("/design/getUnStandProductProcessInfoByUnstandTypeId")
    public ResultModel getUnStandProductProcessInfo(@RequestParam String unStandTypeId){
        return ResultModel.success(unStandProductProcessInfoService.getUnStandProductProcessInfo(unStandTypeId));
    }

    @ApiOperation("分页获取非标柜体工艺工序工时")
    @PostMapping("/design/getPagedUnStandProductProcessInfoByUnstandTypeId")
    public ResultModel getPagedUnStandProductProcessInfo(@RequestBody UnStandProductProcessInfoQueryCriteria queryCriteria){
        return ResultModel.success(unStandProductProcessInfoService.getUnStandProductProcessInfoByPage(queryCriteria));
    }

    @ApiOperation("排程页获取柜体工艺工序工时(含标柜/非标柜)")
    @PostMapping("/design/getPagedProductProcessInfoByProductId")
    public ResultModel getPagedProductProcessInfo(@RequestBody UnStandProductProcessInfoQueryCriteria queryCriteria){
        return ResultModel.success(unStandProductProcessInfoService.getPagedProductProcessInfo(queryCriteria));
    }

    //柜体设计页面设置工艺
    @ApiOperation("设置工艺路径")
    @PostMapping("/design/setBop")
    public ResultModel setBop(@ApiParam(value = "批次柜体id", required = true) @RequestParam String unStandTypeId,
                              @ApiParam(value = "工艺路线Code", required = true) @RequestParam String bopCode) {
        unStandProductProcessInfoService.setBop(unStandTypeId, bopCode);
        return ResultModel.success("设置成功");
    }

    @ApiOperation("根据柜体id获取工艺路径信息")
    @PostMapping("/design/getBop")
    public ResultModel getBop(@ApiParam(value = "批次柜体id", required = true) @RequestParam String unStandTypeId) {

        return ResultModel.success(unStandProductProcessInfoService.getBopInfo(unStandTypeId));
    }


    @ApiOperation("根据项目信息导出非标柜体工时工期模板")
    @PostMapping("/design/exportWorkhourAndCycleByProjectInfo")
    public void exportWorkhourAndCycle(HttpServletResponse response,
                                       @ApiParam(value = "项目id", required = true) @RequestParam String projectInfoId,
                                       @ApiParam(value = "项目名称", required = true) @RequestParam String projectName,
                                       @ApiParam(value = "项目编码", required = true) @RequestParam String projectCode) throws IOException {

        List<UnStandProductWorkHourAndCycleEx> unStandProductWorkHourAndCycleByProjectInfo = unStandProductProcessInfoService.getUnstandProductWorkHourAndCycleByProjectInfo(projectInfoId, projectName, projectCode);
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode(projectName+"工时工序上传模板", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xls");
        //EasyExcel.write(response.getOutputStream(), UnStandProductWorkHourAndCycleEx.class).sheet("sheet1").doWrite(unStandProductWorkHourAndCycleByProjectInfo);
        ExcelWriterBuilder ewb=EasyExcel.write(response.getOutputStream(), UnStandProductWorkHourAndCycleEx.class);
        ewb.excelType(ExcelTypeEnum.XLS);
        ewb.sheet("sheet1").doWrite(unStandProductWorkHourAndCycleByProjectInfo);
    }

    @ApiOperation("根据柜体id导出非标柜体工时工期模板")
    @PostMapping("/design/exportWorkhourAndCycleByProductId")
    public void exportWorkhourAndCycle(HttpServletResponse response,
                                       @ApiParam(value = "柜体id", required = true) @RequestBody List<String> productIds) throws IOException {

        List<UnStandProductWorkHourAndCycleEx> unStandProductWorkHourAndCycleByProjectInfo = unStandProductProcessInfoService.getUnstandProductWorkHourAndCycleByProductIds(productIds);
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("工时工序上传模板", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xls");
        //EasyExcel.write(response.getOutputStream(), UnStandProductWorkHourAndCycleEx.class).sheet("sheet1").doWrite(unStandProductWorkHourAndCycleByProjectInfo);
        ExcelWriterBuilder ewb=EasyExcel.write(response.getOutputStream(), UnStandProductWorkHourAndCycleEx.class);
        ewb.excelType(ExcelTypeEnum.XLS);
        ewb.sheet("sheet1").doWrite(unStandProductWorkHourAndCycleByProjectInfo);
    }


    @ApiOperation("上传非标柜体工时工期文件")
    @PostMapping("/design/uploadWorkhourAndCycle")
    public ResultModel uploadWorkhourAndCycle(@RequestParam("file") MultipartFile[] files) {
        for(MultipartFile file:files) {
            if (file.isEmpty()) {
                throw new BeadminException("请上传文件!");
            }
            unStandProductProcessInfoService.uploadWorkhourAndCycle(file);
        }
        return ResultModel.success("上传成功");
    }

}
